package com.yungu.swift.api.wxapi;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.system.marketing.model.dto.MarketingGiftDto;
import com.yungu.swift.system.marketing.service.MarketingGiftService;
import com.yungu.swift.system.sys.model.dto.SysAccountPassengerDto;
import com.yungu.swift.system.sys.service.SysAccountPassengerService;
import com.yungu.swift.utils.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Optional;

import static com.yungu.swift.api.config.NacosListener.SYS_CONFIG;

@Controller
@RequestMapping("/yue")
public class ViewController {

    @Reference
    private MarketingGiftService marketingGiftService;
    @Reference
    private PassengerService passengerService;
    @Reference
    private SysAccountPassengerService SysAccountPassengerService;

    /**
     * 首页
     *
     * @return wxPage/index
     */
    @RequestMapping("/index")
    public String index() {
        return "wxPage/index";
    }

    /**
     * 登录
     *
     * @param request request
     * @return "wxPage/login
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        return "wxPage/login";
    }

    /**
     * 输入手机号
     *
     * @return wxPage/inputPhone
     */
    @RequestMapping("/inputPhone")
    public String inputPhone() {
        return "wxPage/inputPhone";
    }

    /**
     * 输入手机号
     *
     * @return wxPage/inputFlight
     */
    @RequestMapping("/inputFlight")
    public String inputFlight() {
        return "wxPage/inputFlight";
    }

    /**
     * 起点查询
     *
     * @return wxPage/start
     */
    @RequestMapping("/start")
    public String start() {
        return "wxPage/start";
    }

    /**
     * 接送机查询
     *
     * @return wxPage/start
     */
    @RequestMapping("/addr/transfer")
    public String addrTransfer() {
        return "wxPage/addr_transfer";
    }

    /**
     * 优惠券列表
     *
     * @return wxPage/couponList
     */
    @RequestMapping("/couponlist")
    public String couponlist() {
        return "wxPage/couponList";
    }

    /**
     * 终点查询
     *
     * @return wxPage/end
     */
    @RequestMapping("/end")
    public String end() {
        return "wxPage/end";
    }

    /**
     * 待上车
     *
     * @return wxPage/wtakecar
     */
    @RequestMapping("/wtakecar")
    public String wtakecar() {
        return "wxPage/wtakecar";
    }

    /**
     * 等待接驾
     *
     * @return wxPage/wmeeting
     */
    @RequestMapping("/wmeeting")
    public String wmeeting() {
        return "wxPage/wmeeting";
    }

    /**
     * 等待接驾--预约
     *
     * @return wxPage/wmeeting1
     */
    @RequestMapping("/wmeeting1")
    public String wmeeting1() {
        return "wxPage/wmeeting1";
    }

    /**
     * 等待应答
     *
     * @return wxPage/wreply
     */
    @RequestMapping("/wreply")
    public String wreply() {
        return "wxPage/wreply";
    }

    /**
     * 取消订单
     *
     * @return wxPage/cancel
     */
    @RequestMapping("/cancel")
    public String cancel() {
        return "wxPage/cancel";
    }

    /**
     * 行程中
     *
     * @return wxPage/onway
     */
    @RequestMapping("/onway")
    public String onway() {
        return "wxPage/onway";
    }

    /**
     * 待支付
     *
     * @return wxPage/wpay
     */
    @RequestMapping("/wpay")
    public String wpay() {
        return "wxPage/wpay";
    }

    /**
     * 支付弹窗
     *
     * @return wxPage/paydialog
     */
    @RequestMapping("/paydialog")
    public String paydialog() {
        return "wxPage/paydialog";
    }

    /**
     * 待评价
     *
     * @return wxPage/wrated
     */
    @RequestMapping("/wrated")
    public String wrated() {
        return "wxPage/wrated";
    }

    /**
     * 待评价完成
     *
     * @return wxPage/wrated_success
     */
    @RequestMapping("/wrated_success")
    public String wratedSuccess() {
        return "wxPage/wrated_success";
    }

    /**
     * 评价弹窗
     *
     * @return wxPage/ratedialog
     */
    @RequestMapping("/ratedialog")
    public String ratedialog() {
        return "wxPage/ratedialog";
    }

    /**
     * 已完成
     *
     * @return wxPage/complete
     */
    @RequestMapping("/complete")
    public String complete() {
        return "wxPage/complete";
    }

    /**
     * 评价详情弹窗
     *
     * @return wxPage/ratedetail
     */
    @RequestMapping("/ratedetail")
    public String ratedetail() {
        return "wxPage/ratedetail";
    }

    /**
     * 已取消-无需支付
     *
     * @return wxPage/cancelPay
     */
    @RequestMapping("/cancelPay")
    public String cancelPay() {
        return "wxPage/cancelPay";
    }

    /**
     * 已取消-需支付
     *
     * @return wxPage/cancelNoPay
     */
    @RequestMapping("/cancelNoPay")
    public String cancelNoPay() {
        return "wxPage/cancelNoPay";
    }

    /**
     * 订单取消无取消费，确认页面
     *
     * @return wxPage/cancelConfirmNoPay
     */
    @RequestMapping("/cancelConfirmNoPay")
    public String cancelConfirmNoPay() {
        return "wxPage/cancelConfirmNoPay";
    }

    /**
     * 有取消费用，确认页面
     *
     * @return wxPage/cancelConfirmYesPay
     */
    @RequestMapping("/cancelConfirmYesPay")
    public String cancelConfirmYesPay() {
        return "wxPage/cancelConfirmYesPay";
    }

    /**
     * 费用详情
     *
     * @return wxPage/fareInfo
     */
    @RequestMapping("/fareInfo")
    public String fareInfo() {
        return "wxPage/fareInfo";
    }

    /**
     * 关于我们
     *
     * @return wxPage/about_us
     */
    @RequestMapping("/about")
    public String about() {
        return "wxPage/about_us";
    }

    /**
     * 下载APP中间转跳
     *
     * @return shareActivity/downloadApp
     */
    @RequestMapping("/download")
    public String download() {
        return "shareActivity/downloadApp";
    }

    /**
     * 我的行程
     *
     * @return wxPage/order_list
     */
    @RequestMapping("/order_list")
    public String orderList() {
        return "wxPage/order_list";
    }

    /**
     * 取消订单后查看
     *
     * @return wxPage/cancel_success
     */
    @RequestMapping("/cancel_success")
    public String cancelSuccess() {
        return "wxPage/cancel_success";
    }

    /**
     * 优惠券选择
     *
     * @return wxPage/coupon
     */
    @RequestMapping("/coupon")
    public String coupon() {
        return "wxPage/coupon";
    }

    /**
     * 分享活动司机端邀请成为司机
     *
     * @return shareActivity/driver
     */
    @RequestMapping("/share_driver")
    public String driver() {
        return "shareActivity/driver";
    }

    /**
     * 分享活动司机端邀请成为乘客
     *
     * @return shareActivity/driver-passenger
     */
    @RequestMapping("/driver_passenger")
    public String driverPassenger() {
        return "shareActivity/driver-passenger";
    }

    /**
     * 通用礼包领取
     *
     * @return giftCollection/index
     */
    @RequestMapping("/giftCollection")
    public String giftCollection() {
        return "giftCollection/index";
    }

    /**
     * 分享活动乘客端
     *
     * @return shareActivity/passenger
     */
    @RequestMapping("/share_passenger")
    public String sharePassenger() {
        return "shareActivity/passenger";
    }

    /**
     * 支付弹窗
     *
     * @param uuid     uuid
     * @param modelMap ModelMap
     * @return wxPage/wpay_popup
     */
    @RequestMapping("/wpay_popup/{uuid}")
    public String wpayPopup(@PathVariable("uuid") String uuid, ModelMap modelMap) {
        ResponseData<SysAccountPassengerDto> responseData = SysAccountPassengerService.get(uuid);
        if (responseData.isSuccess() && responseData.getData() != null) {
            SysAccountPassengerDto accountPassengerEntity = responseData.getData();
            BigDecimal presentBalance = Optional.ofNullable(accountPassengerEntity.getPresentBalance()).orElse(BigDecimal.ZERO);
            BigDecimal rechargeBalance = Optional.ofNullable(accountPassengerEntity.getRechargeBalance()).orElse(BigDecimal.ZERO);
            BigDecimal balance = presentBalance.add(rechargeBalance);
            modelMap.put("balance", balance);
        }
        return "wxPage/wpay_popup";
    }

    /**
     * h5
     *
     * @param request request
     * @return wxPage/index_h5
     */
    @RequestMapping("/h5/index")
    public String indexH5(HttpServletRequest request) {
        request.setAttribute("appid", SYS_CONFIG.getAppid());
        return "wxPage/index";

    }

    /**
     * 出租车
     *
     * @return wxPage/index_taxi
     */
    @RequestMapping("/index/taxi")
    public String indexTaxi() {
        return "wxPage/index_taxi";
    }

    /**
     * 接送机
     *
     * @return wxPage/index_taxi
     */
    @RequestMapping("/index/transfer")
    public String indexTransfer() {
        return "wxPage/index_transfer";
    }

    /**
     * 捎话
     *
     * @return wxPage/takeMsg
     */
    @RequestMapping("/takeMsg")
    public String takeMsg() {
        return "wxPage/takeMsg";
    }

    /**
     * 邀请乘客注册
     *
     * @return activity/registerPassenger
     */
    @RequestMapping("/registerPassenger")
    public String registerPassenger() {
        return "activity/registerPassenger";
    }

    /**
     * 跳转到二维码礼包领取界面
     *
     * @param giftUuid
     * @param cid
     * @param model
     * @return
     */
    @RequestMapping("/qrCodeView")
    public String qrCodeView(@RequestParam("giftUuid") String giftUuid, @RequestParam("cid") String cid, Model model) {
        MarketingGiftDto marketingGiftDto = marketingGiftService.get(MapUtils.build("uuid", giftUuid)).getData();
        if (marketingGiftDto != null) {
            String website = SYS_CONFIG.getWebSite();
            model.addAttribute("backgroundImage", website+marketingGiftDto.getBackgroundImage());
            model.addAttribute("buttonImage", website+marketingGiftDto.getButtonImage());
            model.addAttribute("ruleDescription", marketingGiftDto.getRuleDescription());
            model.addAttribute("appid", marketingGiftDto.getAppid());
            model.addAttribute("uuid", marketingGiftDto.getUuid());
            model.addAttribute("cid", cid);
        }
        return "giftCollection/index";
    }

    /**
     * 短连接服务
     *
     * @param shortUrl shortUrl
     * @return String
     */
    @RequestMapping("/short/{shortUrl}")
    public String shortUrl(@PathVariable("shortUrl") String shortUrl) {
        String sortStr = "RYr60mE";
        if (sortStr.equals(shortUrl)) {
            return "redirect:/yue/qrCodeView?giftUuid=d9d2be55f3d847b4a5137549d05d3f4f&appid=c4814be7309811e7b23600163e329925";
        }
        // 暂时这个二维码礼包需要做成短连接的 所以默认跳转到这个链接
        return "redirect:/yue/qrCodeView?giftUuid=d9d2be55f3d847b4a5137549d05d3f4f&appid=c4814be7309811e7b23600163e329925";
    }
}
